﻿@model IEnumerable<MVC_No09.Models.ProInfo>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    <span>物资类型</span>
    @Html.DropDownList("type", ViewBag.TypeList as SelectList);
    <span>物资名称</span>
    @Html.TextBox("text")
    <input type="submit" value="查询" />
    <p>
        @Html.ActionLink("添加", "Create")
    </p>
}
<table class="table">
    <tr>
        <th>
            编号
        </th>
        <th>
            物资名称
        </th>
        <th>
            采购时间
        </th>
        <th>
            是否报废
        </th>
        <th>
            计量单位
        </th>
        <th>
            采购单价
        </th>
        <th>
            物资描述
        </th>
        <th>
            类型名称
        </th>
        <th>操作</th>
    </tr>
    @if (Model.Count() == 0)
    {
        <h1>没有数据</h1>
    }
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>

            <td>
                @Html.DisplayFor(modelItem => item.AddTime)
            </td>
            <td>
                @{
                    var bol = "";
                    if (Convert.ToBoolean(item.IsDestroy))
                    {
                        bol = "已报废";
                    }
                    else
                    {
                        bol = "未报废";
                    }
                }
                @bol
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Unit)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @{
                    var proTypeList = ViewBag.proTypeList as Dictionary<int, string>;
                    var Type = proTypeList.FirstOrDefault(u => u.Key == item.Type).Value;
                }
                @Type
            </td>
            <td>
                @if (!Convert.ToBoolean(item.IsDestroy))
                {
                    <button onclick="Delete(@item.ID)">报废</button>
                }
            </td>
        </tr>
    }

</table>
@section scripts{
    <script>
        fun


        function Delete(id) {
            if (confirm("确定要报废编号为" + id + "的物资吗?")) {
                $.post("/Home/Delete", { id: id }, function (data) {
                    switch (data) {
                        case 0:
                            alert("报废失败");
                            break;
                        case 1:
                            alert("报废成功");
                            location = "/Home/Index";
                            break;
                        case 4:
                            console.log("未知错误");
                            break;
                    }
                })
            }
        }
    </script>
}